package dapps.web.proj;

import java.util.List;
import java.util.ListIterator;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.DateUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.proj.Proj;
import dapps.entity.proj.Type;
import dapps.entity.user.User;
import dapps.service.ServiceException;
import dapps.service.proj.PlanManager;
import dapps.service.proj.ProjManager;


@SuppressWarnings("serial")
@Results( { 
	@Result(name = CRUDActionSupport.RELOAD, location = "proanew.action", type = "redirect")
	})
public class ProanewAction extends CRUDActionSupport<Proj> {

	@Autowired
	private ProjManager projManager;
	@Autowired
	private PlanManager planManager;	


	// 基本属性
	private Proj entity;
	private Long id;
	private Long typeid;
	private Long dutycompid;
	private Integer maxplan;
	private Proj oldentity;
	private ServletContext application = Struts2Utils.getApplication();
	private Integer[] ps  = (Integer[])application.getAttribute("planarray");
		
	private Page<Proj> pageyear = new Page<Proj>(8);
	
	// 角色相关属性

	// 基本属性访问函数 //

	public Proj getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {

		User us  = SpringSecurityUtils.getCurrentUser();
		if (id != null) {
			entity = projManager.get(id);
			if(entity.getType()!=null) typeid = entity.getType().getId();
			if(entity.getDutycomp()!=null) dutycompid = entity.getDutycomp().getId();
		} else {
			entity = new Proj();
			if(ps[1]!=0){
				entity.setYeartime(ps[1]);
				entity.setApplicate(ps[1]*10+1);
			}
			entity.setDutycomp(us);
			entity.setApplicateuser(us);
			dutycompid = us.getId();
		}

	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}


	// CRUD Action 函数 //


	public Page<Proj> getPageyear() {
		return pageyear;
	}

	@SuppressWarnings("deprecation")
	@Override
	public String list() throws Exception {

		Integer thisyear = DateUtils.getCurrentDateTime().getYear()+1900;
		
		maxplan = planManager.getMaxPlaned(thisyear*100);

		Criterion[] criterionsyear = {
				Restrictions.disjunction()
				.add(Restrictions.conjunction()
						.add(Restrictions.eq( "applicateuser.id", SpringSecurityUtils.getCurrentUser().getId() ))
						.add(Restrictions.eq( "yeartime", ps[1]!=0?ps[1]:maxplan+1)))
				.add(Restrictions.conjunction()
						.add(Restrictions.eq( "dutycomp.id", SpringSecurityUtils.getCurrentUser().getId() ))
						.add(Restrictions.between( "yeartime", thisyear*100,maxplan))
						.add(Restrictions.gt("prorank", 0 )))
				,Restrictions.ne("prorank", 9)
		};
		if(ps[1]-maxplan>=2){
			criterionsyear[0]=Restrictions.disjunction()
					.add(Restrictions.conjunction()
							.add(Restrictions.eq( "applicateuser.id", SpringSecurityUtils.getCurrentUser().getId() ))
							.add(Restrictions.eq( "yeartime", ps[1])))
					.add(Restrictions.conjunction()
							.add(Restrictions.eq( "dutycomp.id", SpringSecurityUtils.getCurrentUser().getId() ))
							.add(Restrictions.between( "yeartime", maxplan+1,ps[1]-1)))
					.add(Restrictions.conjunction()
							.add(Restrictions.eq( "dutycomp.id", SpringSecurityUtils.getCurrentUser().getId() ))
							.add(Restrictions.between( "yeartime", thisyear*100,maxplan))
							.add(Restrictions.gt("prorank", 0 )));			
		}
		if(StringUtils.isBlank(pageyear.getOrderBy())){
			pageyear.setOrderBy("yeartime,insertDate");pageyear.setOrder("desc,desc");
		}
		pageyear = projManager.SearchCriterion(pageyear,criterionsyear);
				
		return SUCCESS;
	}
	
	@Override
	public String input() throws Exception {				
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		if(typeid!=null){
			Type type = new Type();
			type.setId(typeid);
			entity.setType(type);
		}
		if(dutycompid!=null&&dutycompid>0){
			User u = new User();
			u.setId(dutycompid);
			entity.setDutycomp(u);
		}
		if(entity.getSl()==0&&entity.getForced()==2) entity.setForced(0);//forced==2 means sl==1
		if(entity.getSl()==1){
			entity.setProrank(3);
			entity.setForced(2);
		}
		entity.setYeartime(ps[1]);
		if (entity.getForced() == 0) {
			if (projManager.isSs(entity)) {
				entity.setProrank(0);// 实施
			} else if (projManager.isCb(entity)) {
				entity.setProrank(2);// 储备
			} else {
				entity.setProrank(1);// 预备
			}
		}

		projManager.saveProj(entity);
		addActionMessage("补报项目成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}
	
	
	public String del() throws Exception {
		try {			
			Criterion[] criterions = {Restrictions.eq("id", id)
					,Restrictions.or(Restrictions.eq("applicateuser.id",SpringSecurityUtils.getCurrentUser().getId())
									,Restrictions.eq("dutycomp.id",SpringSecurityUtils.getCurrentUser().getId()))
			};
			List<Proj> dataList = projManager.FullSearch(null, criterions, null, null);
			ListIterator<Proj> iterator = dataList.listIterator();			
			if ( iterator.hasNext() ) {
				Proj p = (Proj)iterator.next(); 
				p.setProrank(9);
				p.setForced(1);
				projManager.saveProj(p);
				addActionMessage("删除项目成功");
			}else addActionMessage("缺少权限");
			
			
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}


	public Long getTypeid() {
		return typeid;
	}


	public Proj getOldentity() {
		return oldentity;
	}

	public Long getDutycompid() {
		return dutycompid;
	}

	public void setDutycompid(Long dutycompid) {
		this.dutycompid = dutycompid;
	}

	public Integer getMaxplan() {
		return maxplan;
	}



}
